local skel = fk.CreateSkill {
  name = "lb__duanfu",
  tags = { Skill.Wake }
}

Fk:loadTranslationTable {
  ["lb__duanfu"] = "断缚",
  [":lb__duanfu"] = "<b>觉醒技</b>，你进入濒死状态时，你回复体力值至3，获得弃牌堆中每种副类别的装备牌各一张，然后摸三张牌并获得<a href=':lb__yueguan'><font color='red'>〖钺贯〗</font></a>与<a href=':lb__tianji'><font color='red'>〖天击〗</font></a>。",

  ["#lb__duanfu"] = "断缚：请选择每种副类别的装备牌各一张获得之",
  ["#lb__duanfu_slash"] = "断缚：对 %src 使用一张【杀】",
  ["#lb__duanfu_darw"] = "断缚：你可以摸一张牌并令与 %src 相邻的任意名角色横置，然后 %src 进行一次【闪电】判定",
  ["#lb__duanfu_chain"] = "断缚：你可以令与 %src 相邻的任意名角色横置，然后 %src 进行一次【闪电】判定",

  ["$lb__duanfu1"] = "该结束狩猎了！",
  ["$lb__duanfu2"] = "该结束狩猎了！",
}

Fk:addPoxiMethod {
  name = skel.name,
  card_filter = function(to_select, selected, data, extra_data)
    local sub_type = {}
    for _, id in ipairs(selected) do
      table.insert(sub_type, Fk:getCardById(id).sub_type)
    end
    return #selected < #extra_data.default and not table.contains(sub_type, Fk:getCardById(to_select).sub_type)
  end,
  feasible = function(selected, data, extra_data)
    return #selected == #extra_data.default
  end,
  default_choice = function(data, extra_data)
    return extra_data.default
  end,
  prompt = "#lb__duanfu",
}

skel:addEffect(fk.EnterDying, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and target:isAlive()
        and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  can_wake = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if 3 - player.hp > 0 and player:isWounded() then
      room:recover {
        who = player,
        num = 3 - player.hp,
        skillName = skel.name,
        recoverBy = player,
      }
    end
    if player:isAlive() then
      local cards = {}
      local default = {}
      for _, subtype in ipairs({
        Card.SubtypeWeapon,
        Card.SubtypeArmor,
        Card.SubtypeDefensiveRide,
        Card.SubtypeOffensiveRide,
        Card.SubtypeTreasure
      }) do
        local ids = table.filter(room.discard_pile, function(id)
          return Fk:getCardById(id).sub_type == subtype
        end)
        if #ids > 0 then
          table.insertTable(cards, ids)
          table.insert(default, ids[1])
        end
      end
      if #cards > 0 then
        if #cards > 1 then
          cards = room:askToPoxi(player, {
            poxi_type = skel.name,
            cancelable = false,
            data = {
              { "pile_discard", cards },
            },
            extra_data = { default = default }
          })
          if #cards > 0 then
            room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skel.name)
          end
        end
      end
      if player:isAlive() then
        player:drawCards(3, skel.name)
        room:handleAddLoseSkills(player, "lb__yueguan|lb__tianji")
      end
    end
  end,
})

return skel